package models;
import java.text.Format;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.*;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import controllers.JsonDateFormatter;
import controllers.JsonDatetimeFormatter;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import controllers.Helper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
@Entity
@Table(name="vehicle")
public class Vehicle extends Model {
	private static final long serialVersionUID = 1L;
	@Id
	public Integer id; //编号
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date buy_date; //购车日期
	
	public String brand; //厂牌信息
	
	public String engine_number; //发动机号
	
	public String frame_number; //车架号
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date factory_date; //出厂日期
	
	public Integer seat_number; //座位数
	
	public String lwh; //长、宽、高
	
	public String plate_number; //车牌号
	
	public String type; //车辆属性
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date cancle_date; //退牌日期
	
	public String team; //所属车队
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date examine_date; //验车日期
	public static Model.Finder<Integer, Vehicle> find = new Model.Finder<Integer, Vehicle>(Integer.class, Vehicle.class);

	public static Page<Vehicle> page(int page, int pageSize, String sortBy, String order, String [] filterColum, String[] filter) {
		ExpressionList<Vehicle> exp = find.where();
		for(int i= 0; i< filterColum.length; i++) {
			String[] tokens = filter[i].split("\\|", 3);
			if (tokens.length == 0) {
				continue;
			}
			String fieldtype = tokens[0];
			if (tokens.length == 3) { //two values, range search
				if (tokens[1].isEmpty() || tokens[2].isEmpty() ) {
					continue;
				}
				Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
				Object obj1 = Helper.stringToObject(fieldtype, tokens[2]);
				exp = exp.between(filterColum[i], obj0, obj1);
			} else if (tokens.length == 2) { //one value search
				if (tokens[1].isEmpty()) {
					continue;
				}
				if (fieldtype.equalsIgnoreCase("String")) {
					exp = exp.ilike(filterColum[i], "%" + tokens[1] + "%");
				} else {
					Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
					exp = exp.eq(filterColum[i], obj0);
				}
			}
		}
		return exp.orderBy(sortBy + " " + order).findPagingList(pageSize).setFetchAhead(false).getPage(page);
	}
	public void setId(Integer d) {
		id= d;
	}
	public Integer getId() {
		return id;
	}
	public void setBuy_date(Date d) {
		buy_date= d;
	}
	public Date getBuy_date() {
		return buy_date;
	}
	public void setBrand(String d) {
		brand= d;
	}
	public String getBrand() {
		return brand;
	}
	public void setEngine_number(String d) {
		engine_number= d;
	}
	public String getEngine_number() {
		return engine_number;
	}
	public void setFrame_number(String d) {
		frame_number= d;
	}
	public String getFrame_number() {
		return frame_number;
	}
	public void setFactory_date(Date d) {
		factory_date= d;
	}
	public Date getFactory_date() {
		return factory_date;
	}
	public void setSeat_number(Integer d) {
		seat_number= d;
	}
	public Integer getSeat_number() {
		return seat_number;
	}
	public void setLwh(String d) {
		lwh= d;
	}
	public String getLwh() {
		return lwh;
	}
	public void setPlate_number(String d) {
		plate_number= d;
	}
	public String getPlate_number() {
		return plate_number;
	}
	public void setType(String d) {
		type= d;
	}
	public String getType() {
		return type;
	}
	public void setCancle_date(Date d) {
		cancle_date= d;
	}
	public Date getCancle_date() {
		return cancle_date;
	}
	public void setTeam(String d) {
		team= d;
	}
	public String getTeam() {
		return team;
	}
	public void setExamine_date(Date d) {
		examine_date= d;
	}
	public Date getExamine_date() {
		return examine_date;
	}
	public static Vehicle findById(Integer d) {
		return find.byId(d);
	}
	public static String getIdJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Vehicle v: find.all()) {
			node.put(v.getId().toString(), v.getId().toString()); 
		}
		return node.toString();
	}
	public static Vehicle findByBuy_date(Date d) {
		return find.where().eq("buy_date", d).findUnique();
	}
	public static String getBuy_dateJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Vehicle v: find.all()) {
			node.put(v.getBuy_date().toString(), v.getBuy_date().toString()); 
		}
		return node.toString();
	}
	public static Vehicle findByBrand(String d) {
		return find.where().eq("brand", d).findUnique();
	}
	public static String getBrandJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Vehicle v: find.all()) {
			node.put(v.getBrand().toString(), v.getBrand().toString()); 
		}
		return node.toString();
	}
	public static Vehicle findByEngine_number(String d) {
		return find.where().eq("engine_number", d).findUnique();
	}
	public static String getEngine_numberJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Vehicle v: find.all()) {
			node.put(v.getEngine_number().toString(), v.getEngine_number().toString()); 
		}
		return node.toString();
	}
	public static Vehicle findByFrame_number(String d) {
		return find.where().eq("frame_number", d).findUnique();
	}
	public static String getFrame_numberJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Vehicle v: find.all()) {
			node.put(v.getFrame_number().toString(), v.getFrame_number().toString()); 
		}
		return node.toString();
	}
	public static Vehicle findByFactory_date(Date d) {
		return find.where().eq("factory_date", d).findUnique();
	}
	public static String getFactory_dateJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Vehicle v: find.all()) {
			node.put(v.getFactory_date().toString(), v.getFactory_date().toString()); 
		}
		return node.toString();
	}
	public static Vehicle findBySeat_number(Integer d) {
		return find.where().eq("seat_number", d).findUnique();
	}
	public static String getSeat_numberJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Vehicle v: find.all()) {
			node.put(v.getSeat_number().toString(), v.getSeat_number().toString()); 
		}
		return node.toString();
	}
	public static Vehicle findByLwh(String d) {
		return find.where().eq("lwh", d).findUnique();
	}
	public static String getLwhJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Vehicle v: find.all()) {
			node.put(v.getLwh().toString(), v.getLwh().toString()); 
		}
		return node.toString();
	}
	public static Vehicle findByPlate_number(String d) {
		return find.where().eq("plate_number", d).findUnique();
	}
	public static String getPlate_numberJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Vehicle v: find.all()) {
			node.put(v.getPlate_number().toString(), v.getPlate_number().toString()); 
		}
		return node.toString();
	}
	public static Vehicle findByType(String d) {
		return find.where().eq("type", d).findUnique();
	}
	public static String getTypeJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Vehicle v: find.all()) {
			node.put(v.getType().toString(), v.getType().toString()); 
		}
		return node.toString();
	}
	public static Vehicle findByCancle_date(Date d) {
		return find.where().eq("cancle_date", d).findUnique();
	}
	public static String getCancle_dateJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Vehicle v: find.all()) {
			node.put(v.getCancle_date().toString(), v.getCancle_date().toString()); 
		}
		return node.toString();
	}
	public static Vehicle findByExamine_date(Date d) {
		return find.where().eq("examine_date", d).findUnique();
	}
	public static String getExamine_dateJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Vehicle v: find.all()) {
			node.put(v.getExamine_date().toString(), v.getExamine_date().toString()); 
		}
		return node.toString();
	}
}
